﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace MSPToolkit.Behaviors
{
    public class TextBoxWatermarkBehavior : Behavior<TextBox>
    {
        private bool _isWatermarkApplied;
        private Brush _foregroundBrush;
        private Brush _backgroundBrush;
        private Brush _borderBrush;

        private static Brush DefaultBrush = new SolidColorBrush(Colors.LightGray);

        private string _watermarkText = string.Empty;
        public string WatermarkText
        {
            get { return _watermarkText; }
            set { _watermarkText = value; }
        }

        private Brush _watermarkForeground = new SolidColorBrush(Colors.Gray);
        public Brush WatermarkForeground
        {
            get { return _watermarkForeground; }
            set { _watermarkForeground = value; }
        }

        private Brush _watermarkBackground = DefaultBrush;
        public Brush WatermarkBackground
        {
            get { return _watermarkBackground; }
            set { _watermarkBackground = value; }
        }

        private Brush _watermarkBorderBrush = DefaultBrush;
        public Brush WatermarkBorderBrush
        {
            get { return _watermarkBorderBrush; }
            set { _watermarkBorderBrush = value; }
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            _foregroundBrush = AssociatedObject.Foreground;
            _backgroundBrush = AssociatedObject.Background;
            _borderBrush = AssociatedObject.BorderBrush;

            AssociatedObject.GotFocus += AssociatedObjectGotFocus;
            AssociatedObject.LostFocus += AssociatedObjectLostFocus;

            if (string.IsNullOrWhiteSpace(AssociatedObject.Text))
                ApplyWatermark();
        }

        void AssociatedObjectLostFocus(object sender, RoutedEventArgs e)
        {
            if (AssociatedObject.Text.Length == 0)
                ApplyWatermark();
        }

        void AssociatedObjectGotFocus(object sender, RoutedEventArgs e)
        {
            if (_isWatermarkApplied)
                RemoveWatermark();
        }

        private void ApplyWatermark()
        {
            AssociatedObject.Foreground = WatermarkForeground;
            AssociatedObject.Background = WatermarkBackground;
            AssociatedObject.BorderBrush = WatermarkBorderBrush;
            AssociatedObject.Text = WatermarkText;
            _isWatermarkApplied = true;
        }

        private void RemoveWatermark()
        {
            if (_foregroundBrush != null)
                AssociatedObject.Foreground = _foregroundBrush;

            if (_backgroundBrush != null)
                AssociatedObject.Background = _backgroundBrush;
            else
                AssociatedObject.Background = DefaultBrush;

            if (_borderBrush != null)
                AssociatedObject.BorderBrush = _borderBrush;
            else
                AssociatedObject.BorderBrush = DefaultBrush;

            AssociatedObject.Text = string.Empty;
            _isWatermarkApplied = false;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.GotFocus -= AssociatedObjectGotFocus;
            AssociatedObject.LostFocus -= AssociatedObjectLostFocus;
        }
    }
}
